package eu.news.interconnect;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;

import eu.news.config.Debugger;

public final class Message implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7165974981421116146L;

	public static final int VERSION = 1;
	
	int type;
	
	String accessKey;
	
	Entity entity;
	
	String remark;
	
	public Message(int type, String accessKey) {
		super();
		this.type = type;
		this.accessKey = accessKey;
	}

	public int getType() {
		return type;
	}

	public Entity getEntity() {
		return entity;
	}

	public void setEntity(Entity entity) {
		this.entity = entity;
	}

	public String getAccessKey() {
		return accessKey;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}



	public static class TYPE{
		
		public static final int ERROR_INFO = -1;
		
		public static final int RESPONSE = 0;

		public static final int QUERY = 1;
		
		public static final int SEND_CONTENT = 2;
		
		public static final int COMMAND = 4;
	}

	public byte[] getSerializeBytes() throws IOException {
		return Utils.getObjectBytes(this);
	}
	
	public static Message rebuild(byte[] bytes){
		Object message = null;
		try {
			message = Utils.generateObject(bytes);
		} catch (IOException e) {
			Debugger.printStackTrace(e);
		} catch (ClassNotFoundException e) {
			Debugger.printStackTrace(e);
		}
		if (message instanceof Message)
			return (Message) message;
		return null;
	}

	public static Message rebuild(InputStream in){
		Object message = null;
		try {
			message = Utils.generateObject(in);
		} catch (IOException e) {
			Debugger.printStackTrace(e);
		} catch (ClassNotFoundException e) {
			Debugger.printStackTrace(e);
		}
		if (message instanceof Message)
			return (Message) message;
		return null;
	}
}
